package com.yonyou.brigade.order_center.service.impl;

import com.alibaba.fastjson.JSON;
import com.yonyou.brigade.basic_init.service.IBriSysConfigService;
import com.yonyou.brigade.util.HttpClientUtil;
import com.yonyou.brigade.util.operateutil.DateUtils;
import com.yonyou.ybzboot.starter.common.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

public class PushOrder2Note implements Runnable {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    public final static String DD_DATE_FORMAT = "yyyy-MM-dd";

    String tenantId;
    String projecturl;
    String notePushOrders;
    IBriSysConfigService briSysConfigService;

    public PushOrder2Note(String tenantId, String projecturl, String notePushOrders, IBriSysConfigService briSysConfigService) {
        this.tenantId = tenantId;
        this.projecturl = projecturl;
        this.notePushOrders = notePushOrders;
        this.briSysConfigService = briSysConfigService;

    }

    @Override
    public void run() {
        String note_days_before = briSysConfigService.getSysConfigValueByKey("NOTE_DAYS_BEFORE", tenantId, null);
        if (StringUtils.isEmpty(note_days_before)) {
            logger.error(tenantId + "未配置生成记事的提前天数");
            throw new BusinessException("未配置生成记事的提前天数");
        }
        Map<String, String> params = new HashMap<>();
        params.put("tenant", tenantId);
        params.put("beginDate", DateUtils.plusDay(DateUtils.getCurrentDateDay(), -Integer.valueOf(note_days_before), DD_DATE_FORMAT));
        params.put("endDate", DateUtils.plusDay(DateUtils.getCurrentDateDay(), -1, DD_DATE_FORMAT));
        HttpClientUtil.doPostJson(projecturl + notePushOrders, JSON.toJSONString(params));
    }
}
